package TransactionImplementation

import (
	"PaySalary/DataBase"
	"PaySalary/ModelImplementation"
	"PaySalary/TransactionApplication"
	"fmt"
	"log"
	"reflect"
	"strconv"
	"time"
)

type SalesReceiptTransaction struct {
	TransactionApplication.Transaction
	itsEmployeeId int
	itsSalesDate time.Time
	itsAmount float64
}

func (s *SalesReceiptTransaction) NewSalesReceiptTransaction(saleDate time.Time, amount float64, empid int) {
	s.itsSalesDate = saleDate
	s.itsAmount = amount
	s.itsEmployeeId = empid
}

func (s *SalesReceiptTransaction)Execute()  {
	e := DataBase.PayrollDatabase.GetEmployee(s, s.itsEmployeeId)

	if e != nil {
		pc := e.GetClassification()
		if reflect.TypeOf(pc) == reflect.TypeOf(ModelImplementation.CommissionedClassification{}){
			cc := pc.(*ModelImplementation.CommissionedClassification)
			sr := ModelImplementation.NewSalesReceipt(s.itsSalesDate,s.itsAmount)
			cc.AddSalesReceipt(sr)
		} else {
			log.Println(fmt.Errorf(DataBase.PayrollExceptionMessage.NotCommissionedClassification + strconv.Itoa(e.GetEmployeeId())))
		}
	} else {
		log.Println(fmt.Errorf(DataBase.PayrollExceptionMessage.EmployeeNotExist + strconv.Itoa(e.GetEmployeeId())))
	}
}